﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ResourceCollectionDao;
using ResourceCollectionDomain;
using SpiderCore.Service;
using SpiderCore.Utility;

namespace ResourceCollectionSys.Service
{
    public class ResourceManageService
    {
        public static string StartToCatchResource(List<string> sites)
        {
            SpiderService.ReSetSpiderConfig();
            if (sites.Contains("BoKu"))
                SpiderService.AddUrlToTodoQueue(new[] { "http://www.bookuu.com/" }, "book", "BoKu");
            if (sites.Contains("KanKan"))
                SpiderService.AddUrlToTodoQueue(new[] { "http://www.kankan.com/" }, "video", "KanKan");
            VisitedDao.DeleteCanReCatchedUrlsFromVisited();
            SpiderService.CommandSpiderToWork();
            AntService.CommandAntCaptainToWork();
            return "";
        }

        public static bool SaveTask(out int count)
        {
            try
            {
                var urls = SpiderService.GetAllToTodoQueueUrls();
                count = urls.Count;
                ToDoDao.DeleteAll();
                var bokuurls = urls.Where(x => x.Contains("bookuu.com")).ToList();
                ToDoDao.AddUrls(bokuurls, "BoKu");
                var kankanurls = urls.Where(x => x.Contains("kankan.com")).ToList();
                ToDoDao.AddUrls(kankanurls, "KanKan");
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("保存任务现状异常", ex);
                count = 0;
                return false;
            }

        }

        public static bool Recovertask(out int count)
        {
            try
            {
                var boKuurls = ToDoDao.GetAll("BoKu");
                count = boKuurls.Length;
                SpiderService.AddUrlToTodoQueue(boKuurls, "book", "BoKu");
                var kankanurls = ToDoDao.GetAll("KanKan");
                count = +kankanurls.Length;
                SpiderService.AddUrlToTodoQueue(kankanurls, "video", "KanKan");
                return true;
            }
            catch (Exception)
            {
                count = 0;
                return false;
            }
        }

        public static void AddSiteSeed(string[] seedUrl, string flag, string route)
        {
            SpiderService.AddUrlToTodoQueue(seedUrl, flag,route);
        }

    }
}
